/**
 * 
 */
package com.oolong.account.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.oolong.account.model.InItemModel;
import com.oolong.account.model.OutItemModel;
import com.oolong.account.model.UserModel;
import com.oolong.account.service.InManage;
import com.oolong.account.service.OutManage;
import com.oolong.account.service.StaticManage;
import com.oolong.account.vo.TotalStaticVo;

/**
 * @author beralee
 * 
 */
@Controller
@RequestMapping("/index.do")
public class IndexController {
	@Autowired
	private OutManage outManage;
	@Autowired
	private StaticManage staticManage;
	@Autowired
	private InManage inManage;
	String message = " ";

	@RequestMapping(method = RequestMethod.GET)
	public String process(ModelMap model, HttpServletResponse response,
			HttpSession session) {
		UserModel userModel = (UserModel) session.getAttribute("user");
		int user_id = userModel.getId();
		model.addAttribute("user_id", userModel.getId());
		model.addAttribute("username", userModel.getName());
		TotalStaticVo totalStaticVo = staticManage
				.TotalLastStaticService(user_id);
		if (totalStaticVo.getLastMoney() != null) {
			model.addAttribute("money", totalStaticVo.getLastMoney());
		} else {
			model.addAttribute("money", 0);
		}
		// 获得支出前20项
		List<OutItemModel> outItemModels = outManage.query(user_id);
		model.addAttribute("outitems", outItemModels);
		// 获得收入前3项
		List<InItemModel> inItemModels = inManage.query(user_id);
		model.addAttribute("initems", inItemModels);
		Date dt = new Date();
		DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		String nowTime = df.format(dt);
		model.addAttribute("nowTime", nowTime);
		model.addAttribute("message", message);
		response.setContentType("text/html;charset=UTF-8");
		return "index";
	}
}
